package com.itheima.service.system.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.system.UserDao;
import com.itheima.domain.system.User;
import com.itheima.service.system.UserService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    
    @Autowired
    private UserDao userDao;

    public List<User> findAll(String companyId) {
        return userDao.findAll(companyId);
    }

    public PageInfo<User> findByPage(Integer pageNum,Integer pageSize,String companyId) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> list = userDao.findAll(companyId);
        return new PageInfo<User>(list,5);
    }

    public User findById(String id) {
        return userDao.findById(id);
    }

    public void save(User user) {
        user.setPassword(new Md5Hash(user.getPassword(),user.getEmail(),1).toString());
        userDao.save(user);
    }

    public void update(User user) {
        if (!user.getPassword().equals(userDao.findById(user.getId()).getPassword())) {
            user.setPassword(new Md5Hash(user.getPassword(),user.getEmail(),1).toString());
        }
        userDao.update(user);
    }

    public void delete(String id) {
        userDao.delete(id);
    }

    public User findByEmail(String email) {
        return userDao.findByEmail(email);
    }
}
